<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;

class MeetingRoom extends Model
{
    protected $table = 'meeting_room';
    protected $guarded = [];

    protected $appends = ['color'];

    // 会议室
    const ROOM_OPTION = [
        ['key' => 1, 'label' => '会议室一'],
        ['key' => 2, 'label' => '会议室二'],
        ['key' => 3, 'label' => '会议室三'],
    ];

    // 新增会议的字段
    const LIGHTBOX_SECTIONS = [
        ['name' => '会议室', 'height' => 30, 'map_to' => 'room_id', 'type' => 'select', 'options' => self::ROOM_OPTION],
        ['name' => '部门', 'height' => 33, 'map_to' => 'department', 'type' => 'textarea',],
        ['name' => '会议主题', 'height' => 100, 'map_to' => 'text', 'type' => 'textarea', 'focus' => true],
        ['name' => 'time', 'height' => 72, 'map_to' => 'auto', 'type' => 'calendar_time',]
    ];

    const AGO_COLOR = 'gray';
    const OWN_COLOR = '#38c172';

    public function getColorAttribute()
    {
        $color = '';
        if (now()->gt($this->end_date)) {
            $color = self::AGO_COLOR;
        } elseif (Auth::user() && Auth::user()->id === $this->create_user_id) {
            $color = self::OWN_COLOR;
        }

        return $color;
    }

    public function user()
    {
        return $this->hasOne(User::class, 'id', 'create_user_id');
    }

}
